layui.define(['table', 'form', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , admin = layui.admin;

    table.render({
        elem: '#LAY-matches-match-manage',
        toolbar: '#toolbaar',
        url: '/admin/matchlives/lists',
        cols: [[
            { type: 'checkbox', fixed: 'left' },
            { field: 'match_id', width: 100, title: '比赛ID', sort: true, fixed: 'left' },
            {
                field: '', width: 110, title: '比赛类型', sort: true, fixed: 'left', templet: function (d) {
                    return $SPORTS[d.sport_id] !== undefined ? $SPORTS[d.sport_id] : "";
                }
            },
            { field: 'comp', title: '所属赛事', width: 200 },
            { field: 'home', title: '主队', width: 200 },
            { field: 'away', title: '客队', width: 200 },
            {
                field: '', title: '比赛状态', width: 100, templet: function (d) {
                    if ($ALLSTATUS[d.sport_id] !== undefined) {
                        return $ALLSTATUS[d.sport_id][d.match_status];
                    }

                    return "";
                }
            },
            {
                field: '', title: '比赛时间', width: 200, templet: function (d) {
                    return formatTimeStamp(d.match_time);
                }
            },
            {
                field: 'pc_link', title: 'm3u8地址(N主播用)', width: 200, templet: function (d) {
                    return '<a href="javascript:;" data-clipboard-text="" class="font-primary" title="点击复制" lay-event="copyCoverUhdUrl">' + d.pc_link + '</a>';
                }
            },
            {
                field: 'uhd_link', title: '超清直播地址', width: 200, templet: function (d) {
                    return '<a href="javascript:;" data-clipboard-text="" class="font-primary" title="点击复制" lay-event="copyCoverUhdUrl">' + d.uhd_link + '</a>';
                }
            },
            {
                field: 'hd_link', title: '高清直播地址', width: 200, templet: function (d) {
                    return '<a href="javascript:;" data-clipboard-text="" class="font-primary" title="点击复制" lay-event="copyCoverHddUrl">' + d.hd_link + '</a>';
                }
            },
            {
                field: 'sd_link', title: '标清直播地址', width: 200, templet: function (d) {
                    return '<a href="javascript:;" data-clipboard-text="" class="font-primary" title="点击复制" lay-event="copyCoverSddUrl">' + d.sd_link + '</a>';
                }
            },
            {
                title: "操作",
                width: 150,
                align: "right",
                fixed: "right",
                toolbar: "#table-matchlives-operation",
            },
        ]]
        , page: true
        , limit: 50
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    table.on('toolbar(LAY-matches-match-manage)', function (obj) {

        var data = table.checkStatus(obj.config.id);

        layer.confirm("确定要将所选赛事设为预约开播吗", function (index) {
            if (obj.event == 'mult_start_live') {
                for (let index = 0; index < data.data.length; index++) {
                    layer.load();
                    admin.req({
                        url: "/admin/matchlives/auto_live",
                        data: { id: data.data[index].match_id },
                        success(res) {
                            layer.closeAll();
                            if (res.code == 0) {
                                layer.msg(res.msg, { icon: 1 });

                            } else {
                                layer.msg(res.msg, { icon: 5 });
                            }
                        },
                        complete(res) {
                            layer.closeAll("loading");
                        }

                    });
                }
                table.reload("LAY-matches-match-manage");
            }

        });
    });


    exports('matchlives', {})
});


function formatTimeStamp(time_stamp) {
    const time = new Date(time_stamp * 1000);
    const Y = time.getFullYear()
    const M = (time.getMonth() + 1).toString().padStart(2, '0')
    const D = time.getDate().toString().padStart(2, '0')
    const h = time.getHours().toString().padStart(2, '0')
    const m = time.getMinutes().toString().padStart(2, '0')
    const s = time.getSeconds().toString().padStart(2, '0')
    return `${Y}-${M}-${D} ${h}:${m}:${s}`
}